<?php

namespace application\Module;

use library\Core\Db\CDbCriteria;
use application\Model\ProductModel;
use application\Model\HouseProductRelatModel;
use application\Comm\Request;
use application\Comm\Func;
use library\Core\Config;

class Product
{

    public function attribute()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'thumb' => '缩略图',
            'number' => '编号',
            'description' => '描述',
            'category_id' => '空间分类',
            'status' => '状态',
            'orderby' => '排序',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
        ];
    }

    /**
     * 用户列表数据
     * @param array $condition
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function getList($select = [], $category_id = 1, $limit = 12)
    {
        $criteria = new CDbCriteria();
        $criteria->select($select);
        //搜索
        $style_id = Request::getParam('style_id');
        $acreage_id = Request::getParam('acreage_id');
        $function_id = Request::getParam('function_id');
        $price_id = Request::getParam('price_id');
        $price_max = Request::getParam('price_max');
        $price_min = Request::getParam('price_min');
        $tag_id = (int)Request::getParam('tag_id');
        $key = Request::getParam('key');

        $prices = Config::getParamsVal('PRICE_ALL');
        if ($key != '') {
        	//echo $key;exit;
            $criteria->addSearchCondition('name', $key, true, 'or');  //查询名称
            $criteria->addSearchCondition('number', $key, true, 'or');//查询编号
        }
        if ($tag_id) {
            $criteria->addRightJoin(\application\Table\ProductTagRelat::getInstance(), 'product_tag_relat.product_tag_id = ' . $tag_id . ' and product.id=product_tag_relat.product_id', 'id');
        }

        if ($function_id) $criteria->addEqCondition('function_id', $function_id);
        if ($style_id) $criteria->addEqCondition('style_id', $style_id);
        if ($acreage_id) $criteria->addEqCondition('acreage_id', $acreage_id);
        if ($price_max && $price_min) {
            $criteria->addGteCondition('price', intval($price_min));
            $criteria->addLteCondition('price', intval($price_max));
        }
       
        if ($price_id) {
            $price_max = isset($prices[$category_id][$price_id]) ? $prices[$category_id][$price_id]['max'] : 999999999;
            $price_min = isset($prices[$category_id][$price_id]) ? $prices[$category_id][$price_id]['min'] : 0;
           
            $criteria->addGteCondition('price', $price_min);
            $criteria->addLteCondition('price', $price_max);
        }


        $page['now_page'] = Request::getParam('p') ? Request::getParam('p') : 1;
        $page['list_rows'] = Request::getParam('list_rows') ? Request::getParam('list_rows') : $limit;
        $offset = ($page['now_page'] - 1) * $page['list_rows'];

        //排序
        $orderby = (int)Request::getParam('orderby');
        $ordertype = Request::getParam('ordertype');
       
       
        $orderFields = [1 => 'orderby', 2 => 'price', 3 => 'update_time', 4 => 'view',5=>'acreage_id'];
        $orderField = isset($orderFields[$orderby]) ? $orderFields[$orderby] : 'orderby';
        
        if ($category_id) $criteria->addEqCondition('category_id', $category_id);
        $criteria->addEqCondition('is_del', 0);
        $criteria->addEqCondition('status', 1);

        if ($ordertype == 'asc') {
            $criteria->addOrderAsc($orderField);
        } else {
            $criteria->addOrderDesc($orderField);
        }
        $adminModel = new ProductModel();
        $page['total_rows'] = $adminModel->getCount($criteria);

        $criteria->setOffset($offset);
        $criteria->setLimit($page['list_rows']);

        $data = $adminModel->getListForArray($criteria);
        return ['data' => $data, 'page' => $page];
    }

    /**
     * 全屋定制产品列表
     * @param array $condition
     */
    public function getShow($id, $select = ['*'])
    {
        $criteria_product = new CDbCriteria();


        $criteria_product->select($select);

        $criteria_product->addEqCondition('is_del', 0);
        $criteria_product->addEqCondition('house_id', $id);
        $criteria_product->addLeftJoin(\application\Table\Product::getInstance(), 'house_product_relat.product_id=product.id', '*');
        $house = new HouseProductRelatModel();
        return $house->getListForArray($criteria_product);
    }


    /**
     * 根据多个ID获取产品
     * @param array $condition
     */
    public function getListByIds($ids, $select = ['id', 'name', 'thumb', 'price', 'category_id','price_id', 'price_ori', 'is_show_price', 'number', 'description','unit'])
    {
    	$price_all = Config::getParamsVal('PRICE_ALL');
        $criteria = new CDbCriteria();
        $criteria->select($select);
        $criteria->addEqCondition('is_del', 0);
        $criteria->addEqCondition('status', 1);
        $criteria->addInCondition('id', $ids);
        $criteria->setLimit(5);
        $productModel = new ProductModel();
        $data = $productModel->getListForArray($criteria);
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $data[$k]['thumb'] = Func::imgUrl($v['thumb']);
                $data[$k]['price_show'] = $v['is_show_price'] == 1 ? $v['price'] && $v['unit'] !='' ? $v['price'].$v['unit'] : $v['price'].'元' : @$price_all[$v['category_id']][$v['price_id']]['lable'];
            }
        }
        return $data;
    }

    /**
     * @param $id
     * @return bool
     * 更新产品浏览数量
     */
    public function updateView($id)
    {
        $view = $this->getView($id);
        $view++;
        $criteria = new CDbCriteria();
        $criteria->update(['view'=>$view]);
        $criteria->addEqCondition('id', $id);
        $criteria->addEqCondition('status', 1);
        $buyingModel = new ProductModel();
        $data = $buyingModel->modify($criteria);
        if($data!==false){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $id
     * @return array
     * 获取浏览数量
     */
    public function getView($id)
    {
        $select = [
            'view',
        ];
        $criteria = new CDbCriteria();
        $criteria->select($select);
        $criteria->addEqCondition('id', $id);
        $criteria->addEqCondition('is_del', 0);
        $criteria->addEqCondition('status', 1);
        $productModel = new ProductModel();
        return $productModel->getOne($criteria);
    }
    
    /**
     * 为您推荐列表
     * @param array $condition
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function getRecommend($select = [], $category_id = 0, $limit = 4)
    {
    	$style = Config::getParamsVal('STYLE'); 
    	$criteria = new CDbCriteria();
    	$criteria->select($select);
    	$style_len = count($style);
    	//搜索
    	$style_id = ceil(rand(1, $style_len));
    	
    	if ($style_id) $criteria->addEqCondition('style_id', $style_id);
    	if ($category_id) $criteria->addEqCondition('category_id', $category_id);
    	$adminModel = new ProductModel();
    	$data = $adminModel->getListForArray($criteria);

    	if (count($data)>$limit){
    		shuffle($data);
    		$Recommend = array_slice($data,0,$limit);
    	}else {
    		$Recommend = $data;
    	}
    	return ['data' => $Recommend];
    }


}
